package common

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"server/common/utils"
	"server/pkg/response"
)

var Upload = upload{}

type upload struct{}

func (*upload) UploadImage(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		response.Error(c, "err")
		return
	}

	name, _ := c.GetPostForm("name")
	fileName := fmt.Sprintf("user/%d/%s.png", utils.GetUserId(c), name)
	filePath := fmt.Sprintf("./static/upload/%s", fileName)
	err = c.SaveUploadedFile(file, filePath)
	if err != nil {
		response.Error(c, "update err")
		return
	}

	response.Success(c, gin.H{"file": fileName})
}
